// Haxzs Audio Server
// made by omghaxzs
// 2010

#include "settings.h"
#include "log.h"

settings::settings()
{
	// Define our default settings
	std::map<std::string, std::string> default;

	l->printMsg("Loading settings...", false);

	// Load the settings
	if (!loadSettings())
	{
		// TODO: Error checking
		return;
	}

	l->printMsg("done.", true);
}

settings::~settings()
{
}

bool settings::loadSettings()
{
	// Open the settings file
	settings_str.open("settings", std::fstream::in); // Read flags only

	// Is it open?
	if (!settings_str.is_open())
	{
		// Error opening file
		// TODO: Error Checking
		return false;
	}

	// Alright, let's read this
	char text[256];
	std::string str, name, value;
	memset(text, 0, sizeof(text)); // Initialize

	while (!settings_str.eof())
	{
		settings_str.getline(text, sizeof(text));

		str.assign(text);

		// Is the first character a #?
		if (text[0] == '#')
		{
			// Skip this line, it's a comment
			continue;
		}
		else if (strlen(text) == 0)
		{
			continue;
		}
		else
		{
			// Find the first instance of a ":"
			name = str.substr(0, str.find(":"));

			// Now find the first instance of a ";" from the ":"
			value = str.substr(str.find(":")+1, str.find(";")-str.find(":")-1);

			// Now set the setting
			this->setSetting(name, value);
		}
	}

	settings_str.close();

	return true;
}

std::string settings::getSetting(std::string setting_name)
{
	return set[setting_name];
}

std::string settings::setSetting(std::string setting_name, std::string setting_value)
{
	set[setting_name] = setting_value;

	return setting_name;
}